<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="订单编号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

<!--      <el-form-item>
        <el-date-picker v-model="daterangeLogTime" size="small" style="width: 240px" value-format="yyyy-MM-dd"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
 -->

      <el-form-item prop="logType">
        <el-select v-model="queryParams.logType" placeholder="请选择记录类型" clearable size="small">
          <el-option v-for="dict in dict.type.tms_order_log_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-date-picker v-model="daterangeCreateTime" size="small" style="width: 240px" value-format="yyyy-MM-dd"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="orderLogList" @selection-change="handleSelectionChange" border stripe>
      <el-table-column type="index" width="55" align="center" fixed="left"/>
      <!-- <el-table-column label="日志ID" align="center" prop="id" v-if="true"/> -->
      <el-table-column label="订单号" align="center" prop="orderNo" fixed="left" width="160">
        <template slot-scope="scope">
          <el-link type="primary" @click="handleDetail(scope.row)">{{scope.row.orderNo}}</el-link>
        </template>
      </el-table-column>
      <!-- <el-table-column label="记录时间" align="center" prop="logTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.logTime) }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="经办人ID" align="center" prop="userId" /> -->
      <el-table-column label="经办人" align="center" prop="operator" />
      <el-table-column label="记录类型" align="center" prop="logType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tms_order_log_type" :value="scope.row.logType" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="记录内容" align="center" prop="content" /> -->
      <!-- <el-table-column label="创建人" align="center" prop="createBy" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--  <el-table-column label="删除标识" align="center" prop="delFlag" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="showContent(scope.row)"
            v-hasPermi="['tms:orderLog:edit']"
          >查看</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tms:orderLog:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tms:orderLog:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <el-drawer :visible.sync="drawerOpen">
      <el-card>
        <editor v-model="content"/>
      </el-card>
    </el-drawer>
    <!-- 添加或修改订单日志对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="880px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">

        <el-row :gutter="15">
          <el-col :span="8">
            <el-form-item label="订单ID" prop="orderId">
              <el-input v-model="form.orderId" placeholder="请输入订单ID" />
            </el-form-item>
          </el-col>


          <el-col :span="8">
            <el-form-item label="记录时间" prop="logTime">
              <el-date-picker clearable size="small" v-model="form.logTime" type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择记录时间">
              </el-date-picker>
            </el-form-item>
          </el-col>


          <el-col :span="8">
            <el-form-item label="经办人ID" prop="userId">
              <el-input v-model="form.userId" placeholder="请输入经办人ID" />
            </el-form-item>
          </el-col>
        </el-row>


        <el-row :gutter="15">
          <el-col :span="8">
            <el-form-item label="经办人" prop="operator">
              <el-input v-model="form.operator" placeholder="请输入经办人" />
            </el-form-item>
          </el-col>


          <el-col :span="8">
            <el-form-item label="记录类型" prop="logType">
              <el-select v-model="form.logType" placeholder="请选择记录类型">
                <el-option v-for="dict in dict.type.tms_order_log_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>


          <el-col :span="8">
            <el-form-item label="记录内容">
              <editor v-model="form.content" :min-height="192" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listOrderLog,
    getOrderLog,
    delOrderLog,
    addOrderLog,
    updateOrderLog
  } from "@/api/tms/orderLog";
import Quill from "quill";

  export default {
    name: "OrderLog",
    dicts: ['tms_order_log_type'],
    data() {
      return {
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 订单日志表格数据
        orderLogList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        drawerOpen:false,
        content:'',
        // 记录内容时间范围
        daterangeLogTime: [],
        // 记录内容时间范围
        daterangeCreateTime: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderNo: undefined,
          logTime: undefined,
          userId: undefined,
          operator: undefined,
          logType: undefined,
          createBy: undefined,
          createTime: undefined,
          delFlag: '0'
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询订单日志列表 */
      getList() {
        this.loading = true;
        this.queryParams.params = {};
        if (null != this.daterangeLogTime && '' != this.daterangeLogTime) {
          this.queryParams.params["beginLogTime"] = this.daterangeLogTime[0];
          this.queryParams.params["endLogTime"] = this.daterangeLogTime[1];
        }
        if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
          this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
          this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
        }
        listOrderLog(this.queryParams).then(response => {
          this.orderLogList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          orderId: undefined,
          logTime: undefined,
          userId: undefined,
          operator: undefined,
          logType: undefined,
          content: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          delFlag: undefined
        };
        this.resetForm("form");
      },
      showContent(row){
        this.drawerOpen = true;
        this.content = row.content;
      },
      /** 查看按钮操作 */
      handleDetail(row){
        const id = row.orderId;
        this.$router.push({
          path: '/tms/orderInfo/detail/' + id,
          query: {
            pageNum: this.queryParams.pageNum
          }
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.daterangeLogTime = [];
        this.daterangeCreateTime = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加订单日志";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        const id = row.id || this.ids
        getOrderLog(id).then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = "修改订单日志";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            if (this.form.id != null) {
              updateOrderLog(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addOrderLog(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除订单日志编号为"' + ids + '"的数据项？').then(() => {
          this.loading = true;
          return delOrderLog(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).finally(() => {
          this.loading = false;
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('tms/orderLog/export', {
          ...this.queryParams
        }, `orderLog_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
